﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


namespace DbControllers
{


	/// <summary>
	/// Summary description for ConnectionString
	/// </summary>
	public class ConnectionString
	{
		public ConnectionString ( )
		{
		} //eof constructor

		public static string ReturnCurrent ( )
		{

			string connectionStr = System.Convert.ToString
				( System.Web.HttpContext.Current.Session ["global.connectionStr"] );
			string commingFromURL = System.Web.HttpContext.Current.Request.UrlReferrer.ToString ( );
			Utils.Debugger.WriteIf ( "The commingFrom is " + commingFromURL );
			Utils.Debugger.WriteIf ( "Using the current connection string System.Web.HttpContext.Current.Session [ \"global.connectionStr\" ] : " + 		System.Web.HttpContext.Current.Session ["global.connectionStr"] );
			string toCompareLoginURL = Utils.Str.Formatter.getBaseDir ( ) + "login.aspx";

			//comm -- if the use has not selected particular database than use the web.config default settings
			if ( connectionStr == null )
			{
				return System.Convert.ToString (
				System.Configuration.ConfigurationSettings.AppSettings ["ConnectionString"] );
			} //eof if the connectionStr is null 
			else
				return System.Convert.ToString ( connectionStr );


		} //eof method ReturnCurrent


		public static string ReturnOneByNumber ( object numOfDataBase )
		{

			return System.Convert.ToString ( BL.Conf.Instance.Vars ["db" + numOfDataBase.ToString ( )] );

		} //eof ReturnOneByNumber 

		public static int ReturnNumberByOne ( object connStrToCompare )
		{
			string connectionStrToCompare = System.Convert.ToString ( connStrToCompare );

			for ( int i = 0 ; i < 3 ; i++ )
			{
				string connectionStr = System.Convert.ToString ( BL.Conf.Instance.Vars ["db" + i.ToString ( )] ).Trim ( );
				if ( connectionStr.Equals ( connectionStrToCompare ) )
					return i;
			} //eof if connectionStr is not null

			return 0;			//if other numbers are passed return the default one 
		} //eof method ReturnNumberByOne


	} //eof class ConnectionString
} //eof namespace nsDb